package org.linlinjava.litemall.wx.service;

import cn.binarywang.wx.miniapp.api.WxMaService;
import org.linlinjava.litemall.db.domain.LitemallBenefit;
import org.linlinjava.litemall.db.service.LitemallBenefitService;
import org.linlinjava.litemall.db.service.LitemallSystemConfigService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.util.BalanceConstant;
import org.linlinjava.litemall.db.util.BenefitEventConstant;
import org.linlinjava.litemall.wx.util.IDHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 当时起名有误，实际是受益表而非分享表
 */
@Component
public class WxBenefitService {

    @Autowired
    WxMaService wxMaService;

    @Autowired
    private IDHelper idHelper;

    @Autowired
    private LitemallBenefitService benefitService;

    @Autowired
    private LitemallSystemConfigService systemConfigService;

    @Autowired
    private LitemallUserService litemallUserService;

    /**
     * 记录分享收益并打钱
     * @param sourceId
     * @param followerId
     * @param event
     */
    public void share(Integer sourceId, Integer followerId, Short event) {
        share(sourceId, followerId, event, null, BigDecimal.ZERO, BigDecimal.ZERO);
    }

    /**
     * 记录分享收益并打钱
     * @param sourceId
     * @param followerId
     * @param event
     * @param traget
     */
    public void share(Integer sourceId, Integer followerId, Short event, String traget) {
        share(sourceId, followerId, event, traget, BigDecimal.ZERO, BigDecimal.ZERO);
    }

    /**
     * 记录分享收益并打钱
     * @param sourceId
     * @param followerId
     * @param event
     * @param traget
     */
    public void share(Integer sourceId, Integer followerId, Short event, String traget, BigDecimal amount) {
        share(sourceId, followerId, event, traget, amount, BigDecimal.ZERO);
    }

    /**
     * 记录分享收益并打钱
     * @param sourceId
     * @param followerId
     * @param event
     * @param traget
     */
    public void share(Integer sourceId, Integer followerId, Short event, String traget, BigDecimal amount, BigDecimal invest) {

        try {
            System.out.println("进入分享记录");
            if (followerId == null) {
                System.out.println("没有追随者id");
                return;
            }

            if (sourceId.equals(followerId)) { // 分享不可以是自己
                System.out.println("当前分享人是自己");
                return;
            }
            if (benefitService.checkExist(sourceId, followerId, event)) { // 相同事件已发生
                System.out.println("相同事件已发生过");
                return;
            }
            LitemallBenefit record = new LitemallBenefit();
            record.setSourceId(sourceId);
            record.setFollowerId(followerId);
            record.setEvent(event);
            record.setTarget(traget);
            record.setInvest(invest);
            Map<String, String> shareConfig = systemConfigService.listShare();
            if (BenefitEventConstant.SHARE_ORDER.equals(event)) { // 订单
                String benefitType = shareConfig.get("litemall_share_order_benefit_type");
                if ("1".equals(benefitType)) {
                    record.setBenefit(new BigDecimal(shareConfig.get("litemall_share_order_benefit")));
                } else { // 按比例
                    record.setBenefit(amount.divide(new BigDecimal(shareConfig.get("litemall_share_order_benefit"))).setScale(2, BigDecimal.ROUND_HALF_UP));
                }
            } else if (BenefitEventConstant.EVENT_IN.equals(event)) {
                record.setBenefit(new BigDecimal(shareConfig.get("litemall_share_in_benefit")));
            } else if (BenefitEventConstant.SHARE_LOGIN.equals(event)) {
                record.setBenefit(new BigDecimal(shareConfig.get("litemall_share_login_benefit")));
            } else if (BenefitEventConstant.SHARE_MEMBER.equals(event)) {
                record.setBenefit(new BigDecimal(shareConfig.get("litemall_share_member_benefit")));
            } else {
                record.setBenefit(BigDecimal.ZERO);
            }
            benefitService.add(record);
            litemallUserService.addBalance(sourceId, record.getBenefit(), BalanceConstant.SHARE);
        } catch (Exception e) {
            System.out.println("分享记录出错");
            e.printStackTrace();
            // todo 记录log
        }
    }

    /**
     * 记录拼单受益并打钱
     */
    public void spell(Integer sourceId, Integer followerId, String traget, BigDecimal benefit, BigDecimal invest) {
        System.out.println("进入拼单受益记录");
        LitemallBenefit record = new LitemallBenefit();
        record.setSourceId(sourceId);
        record.setFollowerId(followerId);
        record.setEvent(BenefitEventConstant.EVENT_SPELL);
        record.setTarget(traget);
        record.setBenefit(benefit);
        record.setInvest(invest);
        benefitService.add(record);
        litemallUserService.addBalance(sourceId, record.getBenefit(), BalanceConstant.SPELL);
    }

    /**
     * 记录分销受益并打钱
     */
    public void team(Integer sourceId, Integer followerId, String traget, BigDecimal benefit, BigDecimal invest) {
        System.out.println("进入分销受益记录");
        LitemallBenefit record = new LitemallBenefit();
        record.setSourceId(sourceId);
        record.setFollowerId(followerId);
        record.setEvent(BenefitEventConstant.EVENT_TEAM);
        record.setTarget(traget);
        record.setBenefit(benefit);
        record.setInvest(invest);
        benefitService.add(record);
        litemallUserService.addBalance(sourceId, record.getBenefit(), BalanceConstant.TEAM);
    }
}

